//
//  UserManager.m
//  ImGuider
//
//  Created by llt on 2017/4/25.
//  Copyright © 2017年 imguider. All rights reserved.
//

#import "UserManager.h"

#define kToken @"token"

#define kLoginName @"loginName"
#define kLoginPsw @"loginPsw"
#define kLoginCode @"loginCode"

#define kChid @"chid"

#define kUser @"kUser"

@implementation UserManager
@synthesize token = _token,loginName = _loginName, loginCode = _loginCode, chid = _chid, user = _user;


static UserManager *_sharedManager = nil;
+ (instancetype)sharedManager {
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedManager = [[UserManager alloc] init];
        
    });
    return _sharedManager;
}
// token
- (void)setToken:(NSString *)token {
    
    _token = token;
    [[NSUserDefaults standardUserDefaults] setObject:token forKey:kToken];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (NSString *)token {
    
    if (!_token) {
        _token = [[NSUserDefaults standardUserDefaults] objectForKey:kToken];
    }
    return _token;
}

- (void)setUser:(UserInfo *)user {
    
    _user = user;
    
    NSString *json = [user modelToJSONString];
    [[NSUserDefaults standardUserDefaults] setObject:json forKey:kUser];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (UserInfo *)user {
    
    if (!_user) {
        NSString *json = [[NSUserDefaults standardUserDefaults] objectForKey:kUser];
        
        _user = [UserInfo modelWithJSON:json];
    }
    
    return _user;
}

+ (NSString *)userMd5IdentiferPath:(NSString *)path {
    
    NSString *md5 = [IGUtil md5:[UserManager sharedManager].user.uid];
    
    return [NSString stringWithFormat:@"%@_%@",path,md5 ? md5 : @"imguider"];
}
// loginName
- (void)setLoginName:(NSString *)loginName {
    
    if ([_loginName isEqualToString:loginName]) {
        return;
    }
    
    _loginName = loginName;
    [[NSUserDefaults standardUserDefaults] setObject:loginName forKey:kLoginName];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (NSString *)loginName {
    
    if (!_loginName) {
        _loginName = [[NSUserDefaults standardUserDefaults] objectForKey:kLoginName];
    }
    return _loginName;
}
// loginCode

- (void)setLoginCode:(NSString *)loginCode {
    
    if ([_loginCode isEqualToString:loginCode]) {
        return;
    }
    
    _loginCode = loginCode;
    [[NSUserDefaults standardUserDefaults] setObject:loginCode forKey:kLoginCode];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (NSString *)loginCode {
    
    if (!_loginCode) {
        _loginCode = [[NSUserDefaults standardUserDefaults] objectForKey:kLoginCode];
    }
    return _loginCode;
}


- (void)setChid:(NSString *)chid {
    
    if (![_chid isEqualToString:chid]) {
        _chid = chid;
        [[NSUserDefaults standardUserDefaults] setObject:chid forKey:kChid];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
}

- (NSString *)chid {
    
    if (!_chid) {
        _chid = [[NSUserDefaults standardUserDefaults] objectForKey:kChid];
    }
    
    return _chid;
}

@end
